/**
 * Version 14 mrt 2008 - initial version
 */
package audioreaper.tools;

import audioreaper.machine.MachineContext;

/**
 * <code>AudioUtils</code>.
 *
 * @author <a href="mailto:vincent@nazarene.nl">Vincent van Beveren</a>
 *
 */
public class AudioUtils {

	private AudioUtils() {
	}
	
	/**
	 * Applies panning. Currently only supports stereo buffers. (not mono (duh!), or
	 * surround). 
	 */
	public static void applyPanning(int[] buffer, int panning) {
		if (panning == MachineContext.PANNING_CENTER) {
			return;
		} else if (panning < MachineContext.PANNING_CENTER) {
			int rVolume = MachineContext.PANNING_MAX + panning;
			for (int i = 1; i < buffer.length; i+= 2) {
				buffer[i] = (buffer[i] * rVolume) / MachineContext.PANNING_MAX;								
			}
		} else {
			int lVolume = MachineContext.PANNING_MAX - panning;
			for (int i = 0; i < buffer.length; i+= 2) {
				buffer[i] = (buffer[i] * lVolume) / MachineContext.PANNING_MAX;								
			}
		}
	}

	/**
	 * Applies volume. 
	 */
	public static void applyVolume(int[] buffer, int volume) {
		if (volume == MachineContext.VOLUME_NORMAL) {
			return;
		}
		for (int i = 0; i < buffer.length; i++) {
			buffer[i] = (buffer[i] * volume) / MachineContext.VOLUME_MAX; 			
		}
	}
	

	static int[] generateSine() {
		int[] samples = new int[1000];
		for (int i = 0; i < samples.length; i++) {
			samples[i] = (int)(Math.sin((Math.PI * 2 * i)  / 1000) * 32 * 1024);			
		}
		return samples;
	}
	
}
